package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.HealthArticles;
import com.zhentao.service.HealthArticlesService;
import com.zhentao.vo.Query;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("/health")
@CrossOrigin(origins = "*")
public class jiankangController {
    @Autowired
    private HealthArticlesService healthArticlesService;

    //多条件查询，模糊查询
    @RequestMapping("/findPage")
    public  Result findPage(@RequestBody Query query){
        Page<HealthArticles> page=healthArticlesService.findPage(query);
        return Result.OK(page);
    }
    // ... existing code ...
    //添加健康百科
    @RequestMapping("/add")
    public String add(@RequestBody HealthArticles healthArticles){
        // 确保 publishTime 字段有值，避免数据库报错
        if (healthArticles.getPublishTime() == null) {
            healthArticles.setPublishTime(new Date());
        }
        healthArticles.setPublishTime(new Date());
        boolean save = healthArticlesService.save(healthArticles);
        return save?"添加成功":"添加失败";
    }
// ... existing code ...


    //修改
    @RequestMapping("/update")
    public String update(@RequestBody HealthArticles healthArticles){
        boolean update = healthArticlesService.updateById(healthArticles);
        return update?"修改成功":"修改失败";
    }
    //删除
    @RequestMapping("/delete")
    public String delete(Integer articleId){
        boolean delete = healthArticlesService.removeById(articleId);
        return delete?"删除成功":"删除失败";
    }
    //批量删除
    @RequestMapping("/deleteBatch")
    public String deleteBatch(@RequestBody Integer[] ids) {
        // 错误：Collections.singleton(ids) → 正确：Arrays.asList(ids)
        boolean delete = healthArticlesService.removeByIds(Arrays.asList(ids));
        return delete ? "删除成功" : "删除失败";
    }
}
